﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace rInvoice
{
    public partial class Company : UserControl
    {
        ServerObject mServerObject = null;

        public Company()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }       

        private void FillGridView()
        {
            DataTable clTable = mServerObject.SelectCompanyList(false);
            dataGridView1.DataSource = clTable;
        }

        private void Company_Load(object sender, EventArgs e)
        {
            FillGridView();
        }

        private void newButton_Click(object sender, EventArgs e)
        {
            string messageText = string.Empty;

            CompanyInputPanel AddForm = new CompanyInputPanel();
            AddForm.CreateNew = true;

            DialogResult resultDialog = AddForm.ShowDialog();
            if (resultDialog == DialogResult.OK)
            {
                messageText = "Data was saved";
            }
            else
            {
                messageText = "Sorry, data was not saved";
            }

            MessageBox.Show(messageText);
            FillGridView();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string messageText = string.Empty;

            if (e.RowIndex >= 0)
            {
                int mCompanyID = 0;

                string compID = dataGridView1.Rows[e.RowIndex].Cells["CompanyID"].Value.ToString();

                int.TryParse(compID, out mCompanyID);
                DataRow rowData = mServerObject.SelectCompanyByID(mCompanyID);

                CompanyInputPanel companyIP = new CompanyInputPanel();

                companyIP.CompanyID = mCompanyID;
                companyIP.CompanyNume = rowData["CompanyName"].ToString();

                DateTime regDat = DateTime.Now;
                DateTime.TryParse(rowData["RegistryDate"].ToString(), out regDat);
                companyIP.RegistryDate = regDat;

                companyIP.RegistryNumber = rowData["RegistryNumber"].ToString();
                companyIP.CodFiscal = rowData["CodFisc"].ToString();
                companyIP.CodTVA = rowData["CodTVA"].ToString();
                companyIP.Phone = rowData["Phone"].ToString();
                companyIP.Address = rowData["Address"].ToString();
                companyIP.City = (int)rowData["CityID"];
                companyIP.CountryID = (int)rowData["CountryID"];

                try
                {
                    DialogResult resultDialog = companyIP.ShowDialog();
                    if (resultDialog == DialogResult.OK)
                    {
                        messageText = "Data was saved";
                    }
                    else
                    {
                        messageText = "Sorry, data was not saved";
                    }
                    MessageBox.Show(messageText);
                }
                finally
                {
                    FillGridView();
                }
            }
        }

        private void selectRefresButton_Click(object sender, EventArgs e)
        {
            FillGridView();
        }

        private void accountButton_Click(object sender, EventArgs e)
        {
            string messageText = string.Empty;

            if (dataGridView1.SelectedRows.Count > 0)
            {
                int mCompanyAccID = 0;

                string compID = dataGridView1.Rows[dataGridView1.SelectedRows[0].Index].Cells["CompanyID"].Value.ToString();

                int.TryParse(compID, out mCompanyAccID);
                DataRow rowData = mServerObject.SelectCompanyAccountByID(mCompanyAccID);

                CompanyAccountInputPanel companyAccountIP = new CompanyAccountInputPanel();

                companyAccountIP.AccountID = mCompanyAccID;
                companyAccountIP.CompanyID = (int)rowData["CompanyID"];
                companyAccountIP.AccountName = rowData["AccountName"].ToString();
                companyAccountIP.ValutaID = (int)rowData["ValutaID"];
                companyAccountIP.BankID = (int)rowData["BankID"];
                companyAccountIP.AccountNumber = rowData["AccountNumber"].ToString();
                companyAccountIP.AccountTrezor = rowData["AccountTrezor"].ToString();
                companyAccountIP.BankCorespodentID = (int)rowData["BankCorespodentID"];
                companyAccountIP.AccountCorespodent = rowData["AccountCorespodent"].ToString();
                companyAccountIP.TopID = (int)rowData["TopID"];

                try
                {
                    DialogResult resultDialog = companyAccountIP.ShowDialog();
                    if (resultDialog == DialogResult.OK)
                    {
                        messageText = "Data was saved";
                    }
                    else
                    {
                        messageText = "Sorry, data was not saved";
                    }
                    MessageBox.Show(messageText);
                }
                finally
                {
                    FillGridView();
                }

            }
        }

    }
}
